﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.IBizLogic;
using Yunwei.Web.Models;
using Yunwei.Web.SinalR;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class OrderController : Controller
    {
        IOrderService _orderService;
        IFoodIngredientService _foodIngredientService;
        IDinnerTableService _dinnerTableService;
        public OrderController()
        {
            _orderService = new OrderService();
            _foodIngredientService = new FoodIngredientService();
            _dinnerTableService = new DinnerTableService();
        }
        // GET: RRShop/Order
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult Edit(OrderViewModel ovm)
        {
            var order = _orderService.Find(ovm.Id);
            order.Status = ovm.Status;
            if (_orderService.Update(order))
            {
                if (ovm.Status == 2)
                {
                    var foods = ovm.Foods;
                    foreach(var food in foods)
                    {
                        var ingredients = _foodIngredientService.FindList(50, food.Id, 0);
                        string ingredientString = string.Empty;
                        int i = 0;
                        foreach(var ingredient in ingredients)
                        {
                            if (ingredient != null)
                            {
                                if (i > 0)
                                    ingredientString += "&";
                                ingredientString += string.Format("{0}", ingredient.Id);
                                i++;
                            }
                            //SocketSingleton.CreateInstance().Send("", string.Format("{0}|{1}|{2}|{3}", ovm.Id, food.Id,ingredient.Id,food.Count));
                        }

                        if (!string.IsNullOrEmpty(ingredientString))
                        {
                            SocketSingleton.CreateInstance().Send("", string.Format("{0}|{1}|{2}|{3}", ovm.Id, food.Id, ingredientString, food.Count));
                        }
                    }
                }
                else if (ovm.Status == 5)//订单完成
                {
                    //将座位信息更新
                    var table = _dinnerTableService.Find(order.DinnerTable_Id);
                    table.Status = 0;
                    _dinnerTableService.Update(table);
                }
                else if (ovm.Status == 6)//订单取消
                {
                    //将座位信息更新
                    var table = _dinnerTableService.Find(order.DinnerTable_Id);
                    table.Status = 0;
                    _dinnerTableService.Update(table);

                    //将菜存量还原

                }
                else
                {

                }

                return RedirectToAction("Detail", "Order", new { Area = "RRShop", Id = ovm.Id, restaurant_id = ovm.Restaurant_Id });
            }

            return View("Detail", OrderViewModel.FromOrder(order)); 
        }

        [Authorize]
        public ActionResult Detail(int id, int restaurant_Id)
        {
            ViewBag.Id = restaurant_Id;
            var order = _orderService.Find(id);
            OrderViewModel ovm = OrderViewModel.FromOrder(order);
            return View(ovm);
        }

        [HttpPost]
        public JsonResult OrderList(int pageNo,int pageCount,int restaurant_Id,DateTime? fromDate,DateTime? endDate)
        {
            if (Request.IsAuthenticated&& Request.IsAjaxRequest())
            {
                int totalRecord = 0;
                var orders = _orderService.FindPageList(out totalRecord, 1, 10, "", restaurant_Id, 0, null, null, 0);
                return Json(orders);
            }
            return null;
        }
    }
}